package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.feign.article.IArticleClient;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDimensionDto;
import com.heima.model.wemedia.dtos.WmStatisticsNewsPageDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.DateUtils;
import com.heima.utils.thread.WmThreadLocalUtils;
import com.heima.wemedia.service.WmStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class WmStatisticsServiceImpl implements WmStatisticsService {
    @Autowired
    private IArticleClient articleClient;
    /**
     * 当前登录的用户可以选择指定时间查看文章的一些统计
     * 图片发布量、文章点赞量、文章收藏量、文章点赞量
     */
    @Override
    public ResponseResult newsDimension(String beginDate,String endDate) {
        //参数检查,有一个为空都查询不了
        if (beginDate == null || endDate == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否登录
        WmUser wmUser = WmThreadLocalUtils.getUser();
        if (wmUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //因为传的是字符串类型的时间,所以要进行时间转换
        Date beginTime = DateUtils.stringToDate(beginDate);
        Date endTime = DateUtils.stringToDate(endDate);
        //调用文章远程接口查询点赞收藏数量
        ResponseResult responseResult = articleClient.queryLikesAndConllections(wmUser.getId(),beginTime,endTime);
        Map<String,Object> map = new HashMap<>();
        if (responseResult.getCode().equals(200)){
            String s = JSON.toJSONString(responseResult.getData());
            Map map1 = JSON.parseObject(s, Map.class);
            map.put("likesNum",map1.get("likes") == null? 0:map1.get("likes"));
            map.put("collectNum",map1.get("collections") == null ? 0 : map1.get("collections"));
            map.put("publishNum",map1.get("newsCount") == null ? 0 : map1.get("newsCount"));
        }
        return ResponseResult.okResult(map);
    }

    /**
     * 分页展示文章列表，展示当前时间范围内的具体文章阅读、评论、收藏的数量。
     * @param dto
     * @return
     */
    @Override
    public ResponseResult newsPage(WmStatisticsNewsPageDto dto) {
        //参数检验
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否登录
        WmUser wmUser = WmThreadLocalUtils.getUser();
        if (wmUser == null){
            return ResponseResult.errorResult(501,"请先登录");
        }
        dto.setWmUserId(wmUser.getId());

        PageResponseResult responseResult = articleClient.newPage(dto);
        return responseResult;
    }
}
